import router from './router'
import store from './store'
import { info } from '@/api/login'
import { Message } from 'element-ui'
// 白名单
const whitePage = ['/login', '/404']
router.beforeEach(async(to, from, next) => {
  // 有token
  if (store.state.user.mmToken) {
    //   已登录访问登录页面直接回首页
    if (to.path === '/login') {
      next('/')
    } else {
      // 调取接口看token是否可用
      if (!store.state.user.mmInfo.id) {
        const res = await info()
        store.commit('user/setMmInfo', res.data)
      }
      next()
    }
  } else {
    if (whitePage.includes(to.path)) {
      next()
    } else {
      Message.error('未登录，请先登录！')
      next('/login?redirect=' + to.fullPath)
    }
  }
})
//   if (whitePage.includes(to.fullPath)) {
//     next()
//   } else {
//     if (store.state.user.mmToken) {
//       if (store.state.user.mmInfo) {
//         next()
//       } else {
//         const res = await info()
//         store.commit('user/setInfo', res.data)
//         next()
//       }
//     }
//     next('./login')
//   }
// })
